﻿/*****************************************
 * 
 *  copyright (c) by ylshen
 * 
 *  author : ylshen (syl.lucky@gmail.com)
 * 
 **************************************** */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using JYStockAnalyser.ClientLib;
using JYStockAnalyser.Model;
using JYStockAnalyser.Face;

namespace JYStockAnalyser.Manager
{
    /// <summary>
    /// 指标管理界面
    /// </summary>
    public partial class FrmManageGuideLines : Form
    {
        Stock stock = null;

        public FrmManageGuideLines()
        {
            InitializeComponent();
        }

        private void FrmManageGuideLines_Load(object sender, EventArgs e)
        {            
            m_init();
        }

        /// <summary>
        /// 初始化界面
        /// </summary>
        void m_init()
        {
            this.gvGuideLines.AutoGenerateColumns = false;
            foreach (YearQuarter quarter in ClientCache.Instance.GetAllYearQuarters())
            {
                this.cbQuarter.Items.Add(quarter);
            }

            foreach (string report in ClientCache.Instance.AllTypes.Keys)
            {
                this.cbReports.Items.Add(report);
            }
            this.stockInputer1.BindData();
        }

        private void cbReports_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (m_valueChangedCells.Count != 0)
            {
                if (Tool.ShowQuestionBox("指标值已经改变，确认不更新就离开吗？") != DialogResult.Yes)
                {
                    return;
                }
            }
            stock = (Stock)this.stockInputer1.SelectedItem;
            YearQuarter quarter = (YearQuarter)this.cbQuarter.SelectedItem;
            string report = (string)this.cbReports.SelectedItem;

            if (stock == null)
            {
                Tool.ShowInformationBox("请选择股票!");
                this.stockInputer1.Focus();
                return;
            }

            if (quarter == null)
            {
                Tool.ShowInformationBox("请选择季度!");
                this.cbQuarter.Focus();
                return;
            }

            if (string.IsNullOrEmpty(report))
            {
                Tool.ShowInformationBox("请选择指标类型");
                this.cbReports.Focus();
                return;
            }
            
            StockManageFace face = new StockManageFace();

            object[] result = face.SearchGuideLines(stock.NO, quarter, ClientCache.Instance.AllTypes[report][stock.Kind].ToArray());

            Stock theStock = (Stock)result[0];

            if (stock == null) { Tool.ShowInformationBox("没有此股票代码!"); this.stockInputer1.Focus(); return; }

            this.gvGuideLines.DataSource = (List<GuideLine>)result[1];
            m_logValueChanged = true;
            m_valueChangedCells.Clear();
        }    

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            List<string> stocks = new List<string>();
            List<GuideLine> lines = new List<GuideLine>();
            foreach (DataGridViewCell changedCell in m_valueChangedCells)
            {
                GuideLine line = (GuideLine)changedCell.OwningRow.DataBoundItem;
                stocks.Add(stock.NO);
                lines.Add(line);
            }
            int result = new StockManageFace().InsertORUpdateGuideLine(stocks.ToArray(), lines.ToArray());
            switch (result)
            {
                case 1:
                    m_valueChangedCells.Clear();
                    Tool.ShowInformationBox("更新成功!");
                    break;
                case 110:
                    Tool.ShowInformationBox("更新失败!");
                    break;
            }
        }

        private void gvGuideLines_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (m_logValueChanged)
            {
                DataGridViewCell cell = this.gvGuideLines.Rows[e.RowIndex].Cells[e.ColumnIndex];
                if (!m_valueChangedCells.Contains(cell))
                {
                    m_valueChangedCells.Add(cell);
                }
            }
        }

        bool m_logValueChanged = false;
        List<DataGridViewCell> m_valueChangedCells = new List<DataGridViewCell>();
    }
}
